﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using nanoCMS.Core.Concrete;
using nanoCMS.Core.Membership;
using nanoCMS.Core.WebUI.Models.ViewModels;

namespace nanoCMS.Core.WebUI.Models
{
    public class UserService
    {
        private readonly WorldContext worldContext;
        private readonly UserProvider userProvider; 

        public UserService(WorldContext worldContext)
        {
            this.worldContext = worldContext;
            this.userProvider = worldContext.UserProvider; 
        }

        public void LogOut()
        {
            worldContext.UserProvider.LogOut();
        }

        public bool LogIn(UserLogOn model)
        {
            User user;
            return worldContext.UserProvider.LogOn(model.Email, model.Password, out user) == MembershipReturn.Success;
        }

        public MembershipReturn RegisterUser(UserRegisterVM model)
        {
            User user;
            return worldContext.UserProvider.CreateUser(model.Email, model.Password, model.Username, out user);
        }

        public List<UserVM> GetListOfUsers()
        {
            return worldContext.UserProvider.GetUsers().Select(user => new UserVM
            {
                UserID = user.UserID,
                DisplayName = user.DisplayName,
                Email = user.Email,
                IsActivated = user.IsActivated,
                IsBlocked = user.IsBlocked
            }).ToList();
        }

        public bool ActivateUser(int userID)
        {
            if (worldContext.UserProvider.ActivateUser(userID) != MembershipReturn.Success) return false;
            return true;
        }

        public UserVM GetUser(int userID)
        {
            User user;
            worldContext.UserProvider.GetUser(userID, out user);
            return UserVMFactory(user);
        }

        public UserEditVM BuildEditModel()
        {
            UserVM userVM = GetUser(SessionManagerBase.CurrentUserID);
            string avatar = ""; 
            if(AvatarProvider.AvatarExists(SessionManagerBase.CurrentUserID))
            {
                avatar = AvatarProvider.GenerateAvatarName(SessionManagerBase.CurrentUserID); 
            }
            UserEditVM model = new UserEditVM()
            {
                User = userVM,
                AvatarName = avatar
            };
            return model;
        }

        public bool EditUser(UserVM user, PasswordVM password)
        {
            // try update password
            if(IsPasswordInserted(password) && CheckPasswordMatch(password) 
                && CheckOldPassword(password))
            {
                MembershipReturn passAction = userProvider.ChangeUserPassword(
                    SessionManagerBase.CurrentUserID, password.Password);
            }
            // change DisplayName
            if(!string.IsNullOrEmpty(user.DisplayName))
            {
                MembershipReturn displayNameAction = userProvider.EditUserDisplayName(
                    SessionManagerBase.CurrentUserID, user.DisplayName); 
                if(displayNameAction == MembershipReturn.Success)
                {
                    return true; 
                }
            }
            // cache
            
            return true; // TODO 
        }

        #region Security/Validation (Password; DisplayName)

        public bool CheckDisplayName(string displayName)
        {
            // TODO
            return true; 
        }

        public bool CheckOldPassword(PasswordVM password)
        {
            if(userProvider.CheckUser(SessionManagerBase.CurrentUser.Email, 
                password.OldPassword) == MembershipReturn.Success)
            {
                return true;
            }
            return false;
        }

        public bool IsPasswordInserted(PasswordVM password)
        {
            if(!string.IsNullOrEmpty(password.Password) && 
                !string.IsNullOrEmpty(password.PasswordConfirm))
            {
                return true;
            }
            return false; 
        }

        public bool CheckPasswordMatch(PasswordVM password)
        {
            if (password.Password.Equals(password.PasswordConfirm))
            {
                return true;
            }
            return false;
        }

        #endregion

        #region factories

        public static UserVM UserVMFactory(User user)
        {
            return new UserVM
            {
                Username = user.Username,
                Email = user.Email,
                DisplayName = user.DisplayName,
                IsActivated = user.IsActivated,
                IsBlocked = user.IsBlocked,
                UserID = user.UserID
            };
        }
        #endregion

    }
}
